import React, { Component } from "react";

export default class Son extends Component {
  constructor(props) {
    super(props);
    this.state = {
      parentRef : null
    }
  }

  fn01(str) {
    console.log("我是子组件", str);
  }

  componentDidMount() {
    console.log("子组件：componentDidMount");
  }  
  
  componentDidUpdate() {
    console.log("子组件：componentDidUpdate");
    console.log("this.state.parentRef", this.state.parentRef);
  }

  getParent = (parentRef) => {
    this.setState({
      parentRef:parentRef
    },()=>{
      console.log("this", this);
      console.log("this.state.parentRef", this.state.parentRef);
    })   
  };

  toParent = () => {
    console.log("this", this);
    console.log("this.state.parentRef", this.state.parentRef);

    this.state.parentRef.sonToParent("hello,我是儿子");
  };

  render() {
    return (
      <div>
        <h1>子组件</h1>
        <input type="button" value="给父组件传值" onClick={this.toParent} />
      </div>
    );
  }
}
